package com.smartlifev30.baiwei.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.smartlifev30.R;
import com.smartlifev30.baiwei.view.dialog.NotifyTipsDialog;
import com.smartlifev30.mvp.module.LoginModule;
import com.smartlifev30.mvp.presenter.LoginPresenter;
import com.smartlifev30.mvp.view.ILoginView;
import com.smartlifev30.utils.NetUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import smartlifev30.com.sdk.StatusCode;
import smartlifev30.com.sdk.net.baiwei.NetManager;
import smartlifev30.com.sdk.net.core.ConnectStatus;
import smartlifev30.com.sdk.net.core.IClient;

import static android.provider.Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS;

/**
 * 1.4g登录超时
 * 2.不是wifi也不是4g
 * 3.token过期,token无效
 * 4.没有记录
 * 5.GuideActivity
 *
 * @author tys
 */
public class LoginActivity extends BaseActivity<LoginPresenter> implements ILoginView {

    @BindView(R.id.login_user_name)
    EditText userNameEt;

    @BindView(R.id.login_pwd)
    EditText pwdEt;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        String userName = presenter.getUserName(this);
        userNameEt.setText(userName);
        userNameEt.setSelection(userName.length());
        Intent intent = getIntent();
        String arg1 = intent.getStringExtra("arg1");
        Log.i("0-0-", "进入登录界面的方式:" + arg1);
        int arg2 = intent.getIntExtra("arg2", -1);
        switch (arg2) {
            case StatusCode.TOKEN_OUT_TIME:
                pwdEt.setText("");
                break;
            case StatusCode.TOKEN_NO_USE:
                pwdEt.setText("");
                break;
            default:
                pwdEt.setText(presenter.getPassword(this));
        }

    }


    @Override
    protected LoginPresenter createPresent() {
        return new LoginPresenter(this, new LoginModule());
    }

    @OnClick({R.id.login_login, R.id.login_register, R.id.login_forget_pwd})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.login_login:
                IClient<String, String> client = NetManager.getInstance().getClient();
                ConnectStatus connectStatus = client.getConnectStatus();

                int status = NetUtils.getCurrentNetStatus(getApplicationContext());
                String result;
                switch (status) {
                    case -1:
                        result = "TYPE_NONE";
                        break;
                    case 0:
                        result = "TYPE_MOBILE";
                        break;
                    case 1:
                        result = "TYPE_WIFI";
                        break;
                    default:
                        result = "unknow";
                }
                Log.e("0-0- tys", "当前网络类型:" + result + " 连接状态:" + connectStatus);
//                presenter.loginServer(userNameEt.getText().toString(), pwdEt.getText().toString());
                break;
            case R.id.login_register:
//                toRegisterActivity();

                Intent intent = null;
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
                    intent = new Intent(ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
                }
                startActivity(intent);
                break;
            case R.id.login_forget_pwd:
//                toForgetPwdActivity();
                NotifyTipsDialog build = new NotifyTipsDialog.Builder(this)
                        .content("hahahha哈哈哈")
                        .them("提示")
                        .build();
                build.setContent("hahha哈哈哈!!!");
                build.show();
                break;
            default:
        }
    }

    private void toForgetPwdActivity() {
        //忘记密码和注册共用一个界面
        Intent intent = new Intent(this, RegAndForPwdActivity.class);
        intent.putExtra("flag", RegAndForPwdActivity.FLAG_FORGET_PWD);
        startActivity(intent);
    }

    private void toRegisterActivity() {
        //忘记密码和注册共用一个界面
        Intent intent = new Intent(this, RegAndForPwdActivity.class);
        intent.putExtra("flag", RegAndForPwdActivity.FLAG_REGISTER);
        startActivity(intent);
    }


    @Override
    public void toGatewayActivity(@Nullable String arg1, @Nullable Integer arg2) {
        Intent intent = new Intent(this, GatewayActivity.class);
        if (!TextUtils.isEmpty(arg1)) {
            intent.putExtra("arg1", arg1);
        }
        if (null != arg2) {
            intent.putExtra("arg2", arg2);
        }
        startActivity(intent);
        finish();
    }

}
